package com.zhhl.controller;

import com.jld.base.common.bean.business.RedisBean;
import com.jld.common.log4j.JLDLogger;
import com.jld.common.util.AppContent;
import com.zhhl.redis.RedisManageService;
import com.zhhl.util.RedisJob;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;
import org.springframework.web.servlet.ModelAndView;

@Controller
public class DaPanContoller extends BaseController {

	@Autowired
	private RedisManageService redisManageService;

	public RedisManageService getRedisManageService() {
		if (this.redisManageService == null) {
			this.redisManageService = ((RedisManageService) AppContent
					.getContextBean("redisManageService"));
		}
		return this.redisManageService;
	}

	protected HttpServletRequest getRequest() {
		return ((ServletRequestAttributes) RequestContextHolder
				.getRequestAttributes()).getRequest();
	}

	protected String getParameter(String name) {
		return getRequest().getParameter(name) != null ? getRequest()
				.getParameter(name).toString() : "";
	}

	@RequestMapping({ "/getJsonFromRedis" })
	public void test(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		RedisBean getBean = this.redisManageService.get("HQ_TradeWeb_DATA");

		response.setCharacterEncoding("UTF-8");
		response.setContentType("text/plain");
		response.setHeader("Pragma", "No-cache");
		response.setHeader("Cache-Control", "no-cache");
		response.setDateHeader("Expires", 0L);

		response.addHeader("Access-Control-Allow-Origin", "*");

		if ("IE".equals(request.getParameter("type"))) {
			response.addHeader("XDomainRequestAllowed", "1");
		}

		response.getWriter().print(getBean.getValue());
	}

	@RequestMapping({ "/getJsonFromRedisToPage" })
	public ModelAndView index(HttpServletRequest request,
			HttpServletResponse response) {
		RedisBean getBean = this.redisManageService.get("HQ_TradeWeb_DATA");

		String viewName = "index";
		ModelAndView mv = new ModelAndView(viewName);

		mv.addObject("json", getBean.getValue());
		return mv;
	}

	@RequestMapping({ "/postJsonToRedis" })
	public void json(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		RedisJob.doJson();
	}

	@RequestMapping({ "/health" })
	public void health(HttpServletRequest request, HttpServletResponse response)
			throws IOException {
		JLDLogger.debug("==============CESHI ==开始=============");

		response.setCharacterEncoding("UTF-8");
		response.getWriter().print("系统健康检查：ok");
	}
}